package com.zhentao.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.admin.pojo.AdUserPageDto;
import com.zhentao.aliyun.IDUtils;
import com.zhentao.common.dtos.PageResponseResult;
import com.zhentao.common.dtos.ResponseResult;
import com.zhentao.common.enums.AppHttpCodeEnum;
import com.zhentao.constants.UserThreadLocal;
import com.zhentao.follow.domain.AppUserFollow;
import com.zhentao.mapper.AppUserInfoMapper;
import com.zhentao.service.AppUserFollowService;
import com.zhentao.service.AppUserRealnameService;
import com.zhentao.service.AppUserService;
import com.zhentao.mapper.AppUserMapper;
import com.zhentao.user.domain.AppUser;
import com.zhentao.user.domain.AppUserInfo;
import com.zhentao.user.domain.AppUserMember;
import com.zhentao.user.domain.AppUserRealname;
import com.zhentao.user.dto.IdDto;
import com.zhentao.user.dto.UserInfoDto;
import com.zhentao.user.dto.UserManageDto;
import com.zhentao.user.vo.IdCardVo;
import com.zhentao.user.vo.UserVo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86188
* @description 针对表【app_user】的数据库操作Service实现
* @createDate 2024-08-30 17:17:46
*/
@Service
@Slf4j
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser>
    implements AppUserService{
    @Resource
    private AppUserMapper appUserMapper;

    @Autowired
    private AppUserFollowService appUserFollowService;

    @Autowired
    private AppUserRealnameService appUserRealnameService;;
    @Override
    public ResponseResult index(UserInfoDto dto) {
        List<UserVo> userList = appUserMapper.indexHomeList(dto);
        return ResponseResult.okResult(userList);
    }

    /**
     * 管理端 - 用户管理列表 / 模糊查询真实姓名 / 实名状态分类
     * @param dto
     * @return
     */
    @Override
    public ResponseResult userList(UserManageDto dto) {
        // 条件查询
        LambdaQueryWrapper<AppUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(dto.getNickname()), AppUser::getNickname, dto.getNickname());
        queryWrapper.eq(StringUtils.isNotBlank(dto.getIsAuthentication()), AppUser::getIsAuthentication, dto.getIsAuthentication());

        // 分页查询
        // List<AppUser> list = list(queryWrapper);
        Page<AppUser> page = new Page<>(dto.getOffset(), dto.getPagesize());
        Page<AppUser> page1 = page(page, queryWrapper);
        // 给结果集赋值

        // 返回前端
        return ResponseResult.okResult(page1);
    }

    /**
     * 后台 将用户 禁用、启用
     * @param id 用户id
     * @param status 状态 1正常 2删除
     * @return
     */
    @Override
    @SneakyThrows
    public ResponseResult MangeUser(Integer id, Integer status) {
        AppUser appUser = new AppUser();
        appUser.setId(id);
        appUser.setStatus(status);
        updateById(appUser);
        return ResponseResult.okResult("操作成功");
    }



    /**
     * 后台 对员工详情查看
     * @param id
     * @return
     */

    @Resource
    private AppUserInfoMapper appUserInfoMapper;
    @Override
    public ResponseResult selectUserInfo(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(500,"id不能为空");
        }
        List<AppUserInfo> userInfoList = appUserInfoMapper.selectUserInfo(id);
        return ResponseResult.okResult(userInfoList);
    }

    @Override
    public ResponseResult selectById(Integer id) {
        return null;
    }

    @Override
    public ResponseResult listGuanzhu() {
        AppUser userInfo = UserThreadLocal.getUserInfo();
        if(userInfo==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        List<AppUserFollow> list = appUserFollowService.list(Wrappers.<AppUserFollow>lambdaQuery().eq(AppUserFollow::getUserId, userInfo.getId()));
        List<Integer> collect = list.stream().map(AppUserFollow::getFollowId).collect(Collectors.toList());
        List<AppUser> listGuanzhu = list(Wrappers.<AppUser>lambdaQuery().in(AppUser::getId, collect));
        return ResponseResult.okResult(listGuanzhu);
    }

    @Override
    public ResponseResult Idcard(IdDto dto) {
        System.out.println(dto.getIdNo());
        System.out.println(dto.getName());
        if(ObjectUtil.isEmpty(dto) || StringUtils.isBlank(dto.getIdNo()) ||StringUtils.isBlank(dto.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getIdNo().length()!= 18){
            return ResponseResult.errorResult(501,"身份证号必须18位");
        }
        try {
            IdCardVo idCardVo = IDUtils.postData(dto);
            if(idCardVo.getRespCode().equals("0000")){
                AppUserRealname address = new AppUserRealname();
                address.setUserId(UserThreadLocal.getUserInfo().getId());
                address.setStatus(2);
                address.setIdCard(idCardVo.getIdNo());
                address.setZsName(idCardVo.getName());
                appUserRealnameService.save(address);
                AppUserInfo appUserInfo = new AppUserInfo();
                if(idCardVo.getSex().equals("M")){
                    appUserInfo.setSex(1);
                }
                if(idCardVo.getSex().equals("F")){
                    appUserInfo.setSex(2);
                }
                appUserInfo.setAge(idCardVo.getAge());
                appUserInfo.setUserId(UserThreadLocal.getUserInfo().getId());
                appUserInfo.setDateOfBirth(idCardVo.getBirthday());
                appUserInfoMapper.insert(appUserInfo);
                AppUser user = getById(UserThreadLocal.getUserInfo().getId());
                user.setIsAuthentication(1);
                updateById(user);
                return ResponseResult.okResult("实名成功");
            } else if (idCardVo.getRespCode().equals("0004")) {
                return ResponseResult.errorResult(501, idCardVo.getRespMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("调用身份证接口异常");
        }
        return null;
    }


}




